package com.nsy.ovulatorymonitor.domain;

import com.nsy.ovulatorymonitor.domain.PeridInfoContract.PeridInfoEntry;
import com.nsy.ovulatorymonitor.domain.UserInfoContract.UserInfoEntry;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class OvulatoryMonitorDbHelper extends SQLiteOpenHelper {

	private static final String TEXT_TYPE = " TEXT"; 
	private static final String COMMA_SEP = ","; 
	
	//UserInfo
	private static final String SQL_CREATE_USERINFO = "CREATE TABLE " + UserInfoEntry.TABLE_NAME + " (" +     
			UserInfoEntry._ID + " INTEGER PRIMARY KEY" +  COMMA_SEP+   
			UserInfoEntry.COLUMN_NAME_USERINFO_EMAIL + TEXT_TYPE + COMMA_SEP +    
			UserInfoEntry.COLUMN_NAME_USERINFO_PASSWORD +")";  
	private static final String SQL_DELETE_USERINFO = "DROP TABLE IF EXISTS " + UserInfoEntry.TABLE_NAME;
	//PeridInfo
	private static final String SQL_CREATE_PERIODINFO = "CREATE TABLE " + PeridInfoEntry.TABLE_NAME + " (" +     
			PeridInfoEntry.COLUMN_NAME_PERIOD_ID + " INTEGER PRIMARY KEY" +  COMMA_SEP+   
			PeridInfoEntry.COLUMN_NAME_PERIOD_NAME + TEXT_TYPE + COMMA_SEP +    
			PeridInfoEntry.COLUMN_NAME_PERIOD_STARTDATE +")";
	private static final String SQL_DELETE_PERIODINFO = "DROP TABLE IF EXISTS " + PeridInfoEntry.TABLE_NAME;
	
	public static final int DATABASE_VERSION = 3;     
	public static final String DATABASE_NAME = "OvulatoryMonitor.db";  
	 
	public OvulatoryMonitorDbHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_USERINFO);
		db.execSQL(SQL_CREATE_PERIODINFO);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		 db.execSQL(SQL_DELETE_USERINFO);   
		 db.execSQL(SQL_DELETE_PERIODINFO); 
		 onCreate(db); 
	}
}
